package com.ymatou.open.product.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.open.product.parameter.GetDouyinProductInfoBean;
import com.ymatou.open.product.service.GetDouyinProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.model.thirdpartysync.promoterproductcommission;
import com.ymttest.database.model.thirdpartysync.promotionproduct;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.DefThirdpartysyncWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.EnvSetup;
/**
*链接添加查询商品详情Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetDouyinProductInfo{
	private static GetDouyinProductInfoBean getdouyinproductinfoBean;
	private static GetDouyinProductInfoCall getdouyinproductinfoCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("链接添加查询商品详情Test");
	}

	@Before
	public void caseUp() {
		getdouyinproductinfoBean=new GetDouyinProductInfoBean();
		getdouyinproductinfoCall=new GetDouyinProductInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getdouyinproductinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_001() {
		Logger.start(true,"正确数据，成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/p2960856.html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			
			Logger.verifyEquals("p2960856", getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_002() {
		Logger.start(true,"商品链接为空，失败");
		try {
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
//			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/p2960856.html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			
			Logger.verifyIsContains("Error",getdouyinproductinfoCall.getReturnData(),"返回错误" );
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_003() {
		Logger.start(true,"商品链接不正确，失败");
		try {
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/p212.html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			
			Logger.verifyIsContains("Error",getdouyinproductinfoCall.getReturnData(),"返回错误" );
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_004() {
		Logger.start(true,"pid错误，失败");
		try {
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/p2960856.html");
			getdouyinproductinfoBean.setPid("pi13dabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			
			Logger.verifyIsContains("Error",getdouyinproductinfoCall.getReturnData(),"返回错误" );
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_005() {
		Logger.start(true,"pid为空，失败");
		try {
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/p2960856.html");
//			getdouyinproductinfoBean.setPid("pi1dabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			
			Logger.verifyIsContains("Error",getdouyinproductinfoCall.getReturnData(),"返回错误" );
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_006() {
		Logger.start(true,"添加一个新商品到推广，成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(500);
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			Logger.verifyEquals(productId, getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
			verify(getdouyinproductinfoCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_007() {
		Logger.start(true,"添加一个新商品多次到推广，成功---幂等");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(500);
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			for (int i = 0; i < 10; i++) {
				getdouyinproductinfoCall.callService();			
			}
			Logger.verifyEquals(productId, getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
			verify(getdouyinproductinfoCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_008() {
		Logger.start(true,"添加一个新商品到推广，设置达人费率、查询的达人费率");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			ThirdpartysyncCallService.insertPromoterProductCommission(productId, "pidabcd", 0.6);
			Thread.sleep(2000);
			getdouyinproductinfoBean.setApp_key("appidabcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			Logger.verifyEquals(productId, getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
			verifycomm(getdouyinproductinfoCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_009() {
		Logger.start(true,"添加一个新商品到推广，设置达人费率时，pid错误、还是按照基础费率");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			ThirdpartysyncCallService.insertPromoterProductCommission(productId, "pid4123abcd", 0.6);
			
			Thread.sleep(500);
			getdouyinproductinfoBean.setApp_key("appid1331abcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			Logger.verifyEquals(productId, getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
			verify(getdouyinproductinfoCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDouyinProductInfo_010() {
		Logger.start(true,"添加一个新商品到推广，自动选择最佳优惠券");
		try {
			
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			ThirdpartysyncCallService.insertPromoterProductCommission(productId, "pid4123abcd", 0.6);
			CouponServiceMCallServiceV2 ccm = new CouponServiceMCallServiceV2();
			CreateCouponBean ccbean = ccm.setDefaultPlatformCreateCouponBeanForRecieve();
			ccbean.setProductIds(Arrays.asList(productId));
			ccm.createPlatformCoupon(ccbean);
			Thread.sleep(500);
			
			getdouyinproductinfoBean.setApp_key("appid1331abcd");
			getdouyinproductinfoBean.setSign("aa");
			getdouyinproductinfoBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getdouyinproductinfoBean.setPid("pidabcd");
			
			getdouyinproductinfoCall.setData(getdouyinproductinfoBean);
			getdouyinproductinfoCall.callService();			
			Logger.verifyEquals(productId, getdouyinproductinfoCall.getString("product_id"),"判断商品id");
			
			verify(getdouyinproductinfoCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	private void verify(JsonObject data) throws Exception {
		Logger.comment("数据库验证");
		
		String productId = data.get("product_id").getAsString();
		promotionproduct promotion = new DefThirdpartysyncWapper().selectPromotionproduct(productId).get(0);
		
		YmtProductsI product = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
		System.out.println(product);
		
		Logger.verifyEquals(product.getsPicUrl(), data.get("cover").getAsString(),"验证首图");
		Logger.verifyEquals(product.getsPicUrl(), data.get("images").getAsString().split(",")[0],"验证全部图片");
		Logger.verifyEquals(product.getiStockNum(), data.get("stock_num").getAsInt(),"验证stock_num");
		Logger.verifyEquals(product.getsProduct(), data.get("title").getAsString(),"验证title");
//		Logger.verifyEquals(product.getsProduct(), data.get("market_price").getAsString(),"验证market_price");
//		Logger.verifyEquals(product.getsProduct(), data.get("price").getAsString(),"验证price");
		Logger.verifyEquals(promotion.getCommission().doubleValue()*100, data.get("cos_ratio").getAsDouble(),"验证cos_ratio");
		Logger.verifyEquals(product.getSellerLoginId(), data.get("shop_name").getAsString(),"验证shop_name");
		Logger.verifyEquals(product.getiUserId(), data.get("shop_id").getAsInt(),"验证shop_id");
		Logger.verifyEquals(true, data.get("is_self").getAsBoolean(),"验证is_self");
		
	}
	
	
	private void verifycomm(JsonObject data) throws Exception {
		Logger.comment("数据库验证");
		
		String productId = data.get("product_id").getAsString();
		promotionproduct promotion = new DefThirdpartysyncWapper().selectPromotionproduct(productId).get(0);
		 promoterproductcommission ppc = new DefThirdpartysyncWapper().selectPromoterProductCommission(productId).get(0);	
		YmtProductsI product = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
		Logger.verifyEquals(product.getsPicUrl(), data.get("cover").getAsString(),"验证首图");
		Logger.verifyEquals(product.getsPicUrl(), data.get("images").getAsString().split(",")[0],"验证全部图片");
		Logger.verifyEquals(product.getiStockNum(), data.get("stock_num").getAsInt(),"验证stock_num");
		Logger.verifyEquals(product.getsProduct(), data.get("title").getAsString(),"验证title");
//		Logger.verifyEquals(product.getsProduct(), data.get("market_price").getAsString(),"验证market_price");
//		Logger.verifyEquals(product.getsProduct(), data.get("price").getAsString(),"验证price");
		Logger.verifyEquals(ppc.getCommission().doubleValue()*100, data.get("cos_ratio").getAsDouble(),"验证cos_ratio");
		Logger.verifyEquals(product.getSellerLoginId(), data.get("shop_name").getAsString(),"验证shop_name");
		Logger.verifyEquals(product.getiUserId(), data.get("shop_id").getAsInt(),"验证shop_id");
		Logger.verifyEquals(true, data.get("is_self").getAsBoolean(),"验证is_self");
		
	}
	
	
	
	
	
	
}